<template>
    <div>
         <div style="margin:0 auto;  padding:5px;" v-if="ifShowDetail === 'A'">
                    <div id="operation">
                        <label>起始日期</label>
                        <input type="date" class="input-sm" name="selectdate" id="selectbegindate">
                        <label>结束日期</label>
                        <input type="date" name="selectdate" class="input-sm" id="selectenddate">
                        <label for="">状态</label>
                        <select name="selectcheckstatus" class="input-sm" id="selectcheckstatus">
                            <option value="2">全部</option>
                            <option value="0">未审核</option>
                            <option value="1">已审核</option>
                        </select>
                        <a class="btn btn-theme btn-sm" @click="search">查询</a>
                        <a class="btn btn-theme btn-sm" @click="findmine" >查看我的请购</a>
                        <a class="btn btn-theme btn-sm" @click="calculate">请购统计</a>
                        <a class="btn btn-theme btn-sm" @click= "needtobuy">要我去买</a>
                        <a class="btn btn-theme btn-sm" @click="newpurchase">新建请购</a>
                    </div>
                    <table id="purchase_table" class="table"></table>
                </div>

        <section style="background-color: #fff;" class="container-fluid" id="detail_container" v-else-if="ifShowDetail === 'B'">
                <h4>请购详情</h4>
                <div class="pull-left">
                        <span >主题:</span>
                        <input v-model="title" type="text" style="background-color:transparent; border: none;width: auto" id="title" autofocus placeholder="请输入主题">
                        <span >请购类型</span>
                        <select id="ftype" style="background-color:transparent;border: 0;" v-model="purchaseType" @click="selectType">
                            <option value="日常采购">日常采购</option>
                            <option value="新项目">新项目</option>
                            <option value="大维修">大维修</option>
                            <option value="小维修">小维修</option>
                            <option value="大保养">大保养</option>
                            <option value="小保养">小保养</option>
                            <option value="外修">外修</option>
                        </select>
                        <span id="fixidno_span" v-if="showfix">维修单号：</span>
                        <select name="ftype" id="fixidno_sel" v-model="fixNo" v-if="showfix" style="background-color:transparent">
                            <option v-for="option in options" v-bind:value="option.fidno">
                                {{option.fname}}
                            </option>
                        </select>
                        <span>公司：{{currentCompany}}</span>
                        <span v-if="status === 'old'">日期:{{date}}</span>
                </div>

                
                <div>
                    <a class="btn btn-theme btn-xs" id="purchasedetail_drop" style="float:right; margin-right:5px;" @click="deleteItem">删除该请购产品 </a>
                    <a class="btn btn-theme btn-xs" id="purchasedetail_add" style="float:right; margin-right:5px;" @click="drawItemTable">添加请购产品</a>
                </div>
                <div class="table-responsive" style="clear: both">
                    <table style="margin-bottom: 0; text-align: center" id="purchasedetail_table" class="table table-striped table-condensed table-bordered table-hover">
                        <thead>
                        <tr style="text-align: center">
                            <th class="check"></th>
                            <th>编号</th>
                            <th class="hidden">商品编码</th>
                            <th>商品名</th>
                            <th>型号</th>
                            <th>类型</th>
                            <th>是否固定资产</th>
                            <th>单位</th>
                            <th class="hidden">颜色</th>
                            <th>数量</th>
                            <th>单价</th>
                            <th>金额</th>
                            <th>使用部门</th>
                            <th>使用设备</th>
                            <th>备注</th>
                            
                            <th>购买人</th>
                            <th class="hidden">购买人id</th>
                            <th>初审</th>
                            <th>初审人</th>
                            <th>复审</th>
                            <th>复审人</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="old" v-for="(od,index) in oldDetail" style="padding: 0;margin-top:0;margin-bottom:0;">
                            <td class="check"><input type="checkbox" name="purchasedetail" :value="od.fidno" v-model="oldcheckedids"></td>
                            <td name="fitemid">{{od.fitemidno}}</td>
                            <td name="fitemno" class="hidden">{{od.fitemno}}</td>
                            <td name="fcgn">{{od.fcgn}}</td>
                            <td name="fmodel">{{od.fmodel}}</td>
                            <td name="fdesc8">{{od.fdesc8}}</td>
                            <td name="fdesc9" style="width: 100px;">{{od.fdesc9}}</td>
                            <td name="fdesc7" style="width:40px;">{{od.fdesc7}}</td>
                            <td name="fcolor" class="hidden">{{od.fcolor}}</td>
                            <td><input style="border:0;background-color:transparent;width:25px" @keyup="getsum" type="text" name="qty" v-model="od.fqty"></td>
                            <td><input style="border:0;background-color:transparent;width:70px" @keyup="getsum" type="text" name="price" v-model="od.fprice"></td>
                            <td><input style="border:0;background-color:transparent;width:70px" readonly name="sum" type="text" v-model="od.fsum"></td>
                            <td><input type="text" @click="getUsedept" v-model="od.fusedeptname" name="usedept" style="border:0;background-color:transparent;width:50px;"></td>
                            <td><input type="text" id="usedevice" v-model='od.fusedevice' style="border:0;background-color:transparent;width: 100px;"></td>
                            <td><input style="border:0;background-color:transparent;width:100px" type="text" name="notes" v-model="od.fnotes"></td>
                            <td ><input @click="getPurchaseuser" style="border:0;background-color:transparent;width:50px" type="text" name="purchaseusername" v-model="od.fpurchaseusername"></td>
                            <td class="hidden"><input type="text" name="purchaseuserid" v-model="od.fpurchaseuser"></td>
                            <td>{{od.fyn1}}</td>
                            <td>{{od.fynusername1}}</td>
                            <td>{{od.fyn2}}</td>
                            <td>{{od.fynusername2}}</td>
                        </tr>
                        <tr class="new" v-for="(nd,index) in newDetail">
                            <td class="check"><input type="checkbox" :value="nd.fitemid" v-model="newcheckedids" name="purchasedetail"></td>
                            <td name="fitemid">{{nd.fitemid}}</td>
                            <td name="fitemno" class="hidden">{{nd.fitemno}}</td>
                            <td name="fcgn">{{nd.fcgn}}</td>
                            <td name="fmodel">{{nd.fmodel}}</td>
                            <td name="fdesc8">{{nd.fdesc8}}</td>
                            <td name="fdesc9" style="width: 100px;">{{nd.fdesc9}}</td>
                            <td name="fdesc7" style="width:40px;">{{nd.fdesc7}}</td>
                            <td name="fcolor" class="hidden">{{nd.fcolor}}</td>
                            <td><input style="border:0;background-color:transparent;width:25px" @keyup="getsum" type="text" name="qty" v-model="nd.fqty"></td>
                            <td><input style="border:0;background-color:transparent;width:70px" @keyup="getsum" type="text" name="price" v-model="nd.fcost"></td>
                            <td><input style="border:0;background-color:transparent;width:70px" readonly name="sum" type="text" v-model="nd.fsum"></td>
                            <td><input type="text" name="usedept" @click="getUsedept" v-model="nd.fusedeptname" style="border:0;background-color:transparent;width:50px;"></td>
                            <td><input type="text" name="usedevice" v-model='nd.fusedevice' style="border:0;background-color:transparent; width: 100px;"></td>
                            <td><input style="border:0;background-color:transparent;width:100px" type="text" v-model="nd.fnotes" name="notes"></td>
                            <td ><input @click="getPurchaseuser" style="border:0;background-color:transparent;width:50px" v-model="nd.purchaseusername" type="text" name="purchaseusername"></td>
                            <td class="hidden"><input type="text" name="purchaseuserid" v-model="nd.fpurchaseuser"></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tbody>
                    </table>
                </div> 
                <div>
                    <span style="float:left">说明:</span>
                    <textarea rows="1" style="background-color: transparent; float:left;" class="form-control" id="notes" v-model="notes"></textarea>
                </div>
                <div style="float: left">
                    <label>商品总价：<span id="sum">{{totalsum}}</span><b>&nbsp;元</b></label>
                </div>
                <div style=" height:25px; text-align:center;clear: both">
                    <a class="btn btn-theme btn-xs return" style="float:right; margin-right:5px;" @click="changeDetailShow">返回</a>
                    <a class="btn btn-theme btn-xs print" style="float:right; margin-right:5px;" @click="print">打印</a>
                    <a class="btn btn-theme btn-xs save" style="float:right; margin-right:5px;" @click="save">保存</a>
                </div>
        </section>

        <section v-else-if="ifShowDetail === 'C'">
            <a class="btn btn-theme pull-left cart" @click="getDetail">购物车<i class="fa fa-shopping-cart"></i> <span class="badge">{{purchaseNum}}</span></a>
            <table class="itemtable"></table>
        </section>
        <section v-else = "ifShowDetail === 'D'">
            
        </section>
         <div class="modal fade" id="purchaseuser_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content container-fluid">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">选择购买人</h4>
                                </div>
                                <div class="modal-body row">
                                    <table id="purchaseuser_add_table" class="table table-striped table-bordered table-hover">

                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <input type="checkbox" id="checkforall">设置为全部购买人
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary" @click="setPurchaseuser" id="purchaseuser_add_submit">确定</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal fade" id="usedept_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content container-fluid">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">选择使用部门</h4>
                                </div>
                                <div class="modal-body row">
                                    <table id="usedept_add_table" class="table table-striped table-bordered table-hover">
                                        
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <input type="checkbox" id="checkforalldept">设置为全部使用部门
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary" @click="setUsedept" >确定</button>
                                </div>
                            </div>
                        </div>
                    </div>
</div>
</template>
<script>
    import '../assets/js/fly.js'
    import '../assets/js/print.js'
    import img from '../assets/logo.png'
    export default {
        data() {
            return {
                status: 'new',
                currentPurchase: '',
                currentCompany: localStorage.activeteamname,
                ifShowDetail: 'A',
                url: 'http:\/\/192.168.1.66:808/api/purchase/fetchlistbydeptame',
                tabledata: '',
                title: '',
                date: '',
                notes: '',
                purchaseType: '日常采购',
                showfix: false,
                fixNo: 0,
                purchaseNum: 0,
                deleteArray: [],
                newDetail: [],
                oldDetail: [],
                oldcheckedids: [],
                newcheckedids: [],
                avtiveElement: '',
                totalsum: '',
                options: ''
            }
        },
        methods: {
            drawItemTable() {
                var _this = this
                this.ifShowDetail = 'C'

                function Obj(fitemid, fitemno, fcgn, fmodel, fdesc8, fdesc9, fdesc7, fcolor, fcost, fqty, fsum, purchaseuser, purchaseusername, fusedept, fusedeptname, fusedevice, fnotes) {
                    this.fitemid = fitemid;
                    this.fitemno = fitemno;
                    this.fcgn = fcgn;
                    this.fmodel = fmodel;
                    this.fdesc8 = fdesc8;
                    this.fdesc9 = fdesc9;
                    this.fdesc7 = fdesc7;
                    this.fcolor = fcolor;
                    this.fcost = fcost;
                    this.fqty = fqty;
                    this.fsum = fsum;
                    this.purchaseuser = purchaseuser;
                    this.purchaseusername = purchaseusername;
                    this.fusedept = fusedept;
                    this.fusedeptname = fusedeptname;
                    this.fusedevice = fusedevice;
                    this.fnotes = fnotes
                }
                var operateEvent = {
                    'click a[name="purchase"]': function(event, value, row, index) {
                        var exist = false
                        if (_this.oldDetail.length > 0) {
                            for (var i = 0; i < _this.oldDetail.length; i++) {
                                if (_this.oldDetail[i].fitemidno == row.fidno) {
                                    _this.oldDetail[i].fqty++;
                                    exist = true;
                                    break
                                }
                            }
                        }
                        if (_this.newDetail.length > 0) {
                            for (var i = 0; i < _this.newDetail.length; i++) {
                                if (_this.newDetail[i].fitemid == row.fidno) {
                                    _this.newDetail[i].fqty++;
                                    exist = true;
                                    break
                                }
                            }
                        }
                        if (exist == false) {
                            var myobj = new Obj()
                            myobj.fitemid = row.fidno
                            myobj.fitemno = row.fitemno
                            myobj.fcgn = row.fcgn
                            myobj.fmodel = row.fmodel
                            myobj.fdesc8 = row.fdesc8
                            myobj.fdesc9 = row.fdesc9
                            myobj.fdesc7 = row.fdesc7
                            myobj.fcolor = row.fcolor
                            myobj.fcost = row.fcost
                            myobj.fqty = 1
                            myobj.fsum = row.fcost
                            myobj.purchaseuser = ''
                            _this.newDetail.push(myobj)
                        }

                        var offset = $(".cart").offset();
                        var flyer = $('<img class="u-flyer" src="' + img + '">');
                        console.log(event)
                        flyer.fly({
                            start: {
                                left: event.pageX, //开始位置（必填）#fly元素会被设置成position: fixed 
                                top: event.pageY //开始位置（必填） 
                            },
                            end: {
                                left: offset.left + 10, //结束位置（必填） 
                                top: offset.top + 10, //结束位置（必填） 
                                width: 0, //结束时宽度 
                                height: 0 //结束时高度 
                            },
                            onEnd() {

                                _this.purchaseNum++;
                            }
                        })
                    }
                }
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/item/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('.itemtable').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '编号',
                                halign: 'center'
                            }, {
                                field: 'fitemno',
                                title: '物料编码',
                                halign: 'center'
                            }, {
                                field: 'fcgn',
                                title: '名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'fdesc8',
                                title: '类型',
                                halign: 'center'
                            }, {
                                field: 'fdesc7',
                                title: '单位',
                                halign: 'center'
                            }, {
                                field: 'fcolor',
                                title: '颜色',
                                halign: 'center'
                            }, {
                                field: 'fcost',
                                title: '价格',
                                halign: 'center'
                            }, {
                                field: 'fdesc9',
                                title: '是否固定资产',
                                halign: 'center'
                            }, {
                                field: 'operate',
                                title: '操作',
                                halign: 'center',
                                events: operateEvent,
                                formatter: '<a name="purchase" class="btn btn-success btn-sm">加入购物车</a>'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            showColumns: true,
                            showRefresh: true,
                            showPaginationSwitch: true,
                            singleSelect: false,
                            checkboxHeader: false,
                        });
                    }
                });
            },
            drawTable() {
                var _this = this
                var operateEvent = {
                    'click a[name="detail"]': function(e, value, row, index) {
                        _this.currentPurchase = row.fidno
                        _this.status = 'old'
                        _this.ifShowDetail = 'B'
                        _this.purchaseType = row.ftype
                        _this.getFixs()
                        if (row.fweixiuidno != 0) {
                            _this.showfix = true
                        }
                        _this.fixNo = row.fweixiuidno
                        _this.date = row.fentdte.replace('T', ' ')
                        _this.title = row.ftitle
                        _this.notes = row.fnotes
                        console.log(row.fstatus)
                        if (row.fstatus == '<i class="glyphicon glyphicon-ok"></i>') {
                            _this.$nextTick(function() {
                                $('#purchasedetail_drop').addClass('hidden')
                                $('#purchasedetail_add').addClass('hidden')
                                $('.save').addClass('hidden')
                            })
                        }
                        $.ajax({
                            type: 'POST',
                            url: 'http://192.168.1.66:808/api/purchase/fetchdetailbyid2',
                            data: {
                                'token': localStorage.token,
                                'userid': localStorage.userid,
                                'fidno2': row.fidno
                            },
                            dataType: 'json',
                            crossDomain: true,
                            error: function(request) {
                                _this.$store.commit('showModal','连接错误')
                            },
                            success: function(retdata) {
                                if (retdata.Table1[0].status == false) {} else {
                                    _this.oldDetail = retdata.Table2
                                    _this.newDetail = []
                                    _this.getPurchaseNum()
                                    _this.getsum()
                                }
                            }
                        });
                    }
                }
                $('#purchase_table').bootstrapTable('destroy')
                $('#purchase_table').bootstrapTable({
                    columns: [{
                        field: 'fidno',
                        title: '请购序号',
                        halign: 'center',
                    }, {
                        field: 'ftitle',
                        title: '请购标题',
                        halign: 'center',
                    }, {
                        field: 'fnotes',
                        title: '备注信息',
                        halign: 'center',
                        width: '50px',
                    }, {
                        field: 'fweixiuidno',
                        title: '维修单号',
                        halign: 'center'
                    }, {
                        field: 'fdeptname',
                        title: '请购部门',
                        halign: 'center'
                    }, {
                        field: 'username',
                        title: '请购人',
                        halign: 'center'
                    }, {
                        field: 'fentdte',
                        title: '请购日期',
                        halign: 'center'
                    }, {
                        field: 'fstatus',
                        title: '状态（<i class="glyphicon glyphicon-ok">已审核 </i><i class="glyphicon glyphicon-remove">未审核</i>)',
                        halign: 'center'
                    }, {
                        field: 'fteamid',
                        title: '团队号',
                        visible: false
                    }, {
                        field: 'operate',
                        title: '查看详情',
                        events: operateEvent,
                        formatter: '<a class="btn btn-theme btn-xs" name="detail">查看详情</a>'
                    }],
                    data: this.tabledata,
                    striped: true,
                    pagination: true,
                    selectItemName: 'purchase',
                    sidePagination: 'client',
                    pageNumber: 1,
                    pageSize: 5,
                    pageList: [10, 20, 50],
                    clickToSelect: true
                })
            },
            getData() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: _this.url,
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'yn': $('#selectcheckstatus').val(),
                        'begindatetime': $('#selectbegindate').val(),
                        'enddatetime': $('#selectenddate').val(),
                        'extra.selectfield': '*',
                        'extra.sortfield': 'fidno',
                        'extra.pagesize': 9999999,
                        'extra.pageindex': 1,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(res) {
                        if (res.Table1[0].status == true) {
                            var resp = res
                            for (var i = 0; i < resp.Table2.length; i++) {
                                resp.Table2[i].fentdte = resp.Table2[i].fentdte.substring(0, 10)
                                if (resp.Table2[i].fstatus == '已审核') {
                                    resp.Table2[i].fstatus = '<i class="glyphicon glyphicon-ok"></i>'
                                } else {
                                    resp.Table2[i].fstatus = '<i class="glyphicon glyphicon-remove"></i>'
                                }
                            }
                            _this.tabledata = resp.Table2
                            _this.drawTable()
                        } else {
                            _this.tabledata = ''
                            _this.drawTable()
                        }
                    }
                })
            },
            search() {
                this.url = 'http://192.168.1.66:808/api/purchase/fetchlistbydeptame'
                this.getData()
            },
            findmine() {
                this.url = 'http://192.168.1.66:808/api/purchase/fetchlistbyuserid'
                this.getData()

            },
            calculate() {
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/purchase/fetchlistbydeptame',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteam,
                        'yn': $('#selectcheckstatus').val(),
                        'begindatetime': $('#selectbegindate').val(),
                        'enddatetime': $('#selectenddate').val(),
                        'extra.selectfield': '*',
                        'extra.sortfield': 'fidno',
                        'extra.pagesize': 9999999,
                        'extra.pageindex': 1,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        self.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        var purchase_list = [];
                        var purchasedetail_list = [];
                        var totalprice = 0;
                        var totalprice_pass = 0;
                        for (var i = 0; i < retdata.Table2.length; i++) {
                            purchase_list.push(retdata.Table2[i].fidno)
                        }
                        for (var i = 0; i < purchase_list.length; i++) {
                            $.ajax({
                                type: 'POST',
                                url: 'http://192.168.1.66:808/api/purchase/fetchdetailbyid2',
                                data: {
                                    'token': localStorage.token,
                                    'userid': localStorage.userid,
                                    'fidno2': purchase_list[i],
                                },
                                dataType: 'json',
                                async: false,
                                crossDomain: true,
                                error: function(request) {
                                    self.$store.commit('showModal','连接错误')
                                },
                                success: function(retdata) {
                                    for (var j = 0; j < retdata.Table2.length; j++) {
                                        if (retdata.Table2[j].fyn2 == '通过') {
                                            totalprice_pass += retdata.Table2[j].fsum
                                        }
                                        totalprice += retdata.Table2[j].fsum
                                    }
                                }
                            });
                        }
                        self.$store.commit('showModal',$('#selectbegindate').val().replace('/', '-') + '到' + $('#selectenddate').val().replace('/', '-') + '共请购' + totalprice + '元，审核通过' + totalprice_pass + '元')
                    }
                });
            },
            needtobuy() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/purchase/fetchneedtobuy',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(res) {
                        if (res.Table1[0].status == true) {
                            _this.tabledata = res.Table2
                            _this.drawTable()
                        } else {
                            _this.tabledata = ''
                            _this.drawTable()
                        }
                    }
                });
            },
            newpurchase() {
                this.ifShowDetail = 'B'
                this.status = 'new'
                this.title = ''
                this.notes = ''
                this.oldDetail = []
            },
            changeDetailShow() {
                this.ifShowDetail = 'A'
                this.getData()
                this.showFix = false
                this.options = ''
                this.fixNo = ''
                this.purchaseType = ''

            },
            selectType() {
                if ($('#ftype').val() == "大保养" || $('#ftype').val() == "小保养" || $('#ftype').val() == "大维修" || $('#ftype').val() == "小维修" || $('#ftype').val() == "外修") {
                    this.showfix = true
                    var _this = this
                    this.getFixs()
                        // $('#fixidno_span,#fixidno_sel').removeClass('hidden');
                } else {
                    this.showfix = false
                }
            },
            save() {
                if (this.title == '') {
                    this.$store.commit('showModal','请购主题不能为空');
                    
                } else {
                    var flag = true
                    if (this.newDetail.length > 0) {
                        for (var i = 0; i < this.newDetail.length; i++) {
                            if (this.newDetail[i].purchaseuser == '') {
                                this.$store.commit('showModal','购买人不能为空');
                                flag = false
                                break
                            }
                        }
                    }
                    if (flag == true) {
                        if (this.status == 'old') {
                            if (this.oldDetail.length > 0 || this.newDetail.length > 0) {
                                var _this = this
                                $.ajax({
                                    type: 'POST',
                                    url: 'http://192.168.1.66:808/api/purchase/update',
                                    data: {
                                        'token': localStorage.token,
                                        'userid': localStorage.userid,
                                        'fidno': _this.currentPurchase,
                                        'ftitle': _this.title,
                                        'fnotes': _this.notes,
                                        'ftype':_this.purchaseType
                                    },
                                    dataType: 'json',
                                    crossDomain: true,
                                    error: function(request) {
                                        console.log(request)
                                    },
                                    success: function(retdata) {
                                        console.log(retdata)
                                    }
                                });
                                if (this.oldDetail.length > 0) {
                                    for (var i = 0; i < this.oldDetail.length; i++) {
                                        var _this = this
                                        $.ajax({
                                            type: 'POST',
                                            url: 'http://192.168.1.66:808/api/purchasedetail/update1',
                                            data: {
                                                'token': localStorage.token,
                                                'userid': localStorage.userid,
                                                'fidno': _this.oldDetail[i].fidno,
                                                'fidno2': _this.currentPUrchase,
                                                'fitemidno': _this.oldDetail[i].fitemidno,
                                                'fqty': _this.oldDetail[i].fqty,
                                                'fprice': _this.oldDetail[i].fprice,
                                                'fsum': _this.oldDetail[i].fsum,
                                                'fnotes': _this.oldDetail[i].fnotes,
                                                'fpurchaseuser': _this.oldDetail[i].fpurchaseuser,
                                                'fusedept': _this.oldDetail[i].fusedept,
                                                'fusedevice': _this.oldDetail[i].fusedevice,
                                                'fyndate1': '1990-01-01',
                                                'fyndate2': '1990-01-01',
                                                'fyndate3': '1990-01-01',
                                            },
                                            dataType: 'json',
                                            crossDomain: true,
                                            error: function(request) {

                                            },
                                            success: function(retdata) {

                                            }
                                        });
                                    }
                                }
                                if (this.newDetail.length > 0) {
                                    for (i = 0; i < this.newDetail.length; i++) {
                                        var _this = this
                                        $.ajax({
                                            type: 'POST',
                                            url: 'http://192.168.1.66:808/api/purchasedetail/add',
                                            data: {
                                                'token': localStorage.token,
                                                'userid': localStorage.userid,
                                                'fidno2': _this.currentPurchase,
                                                'fitemidno': _this.newDetail[i].fitemid,
                                                'fqty': _this.newDetail[i].fqty,
                                                'fprice': _this.newDetail[i].fcost,
                                                'fsum': _this.newDetail[i].fsum,
                                                'fnotes': _this.newDetail[i].fnotes,
                                                'fpurchaseuser': _this.newDetail[i].purchaseuser,
                                                'fusedept': _this.newDetail[i].fusedept,
                                                'fusedevice': _this.newDetail[i].fusedevice
                                            },
                                            dataType: 'json',
                                            crossDomain: true,
                                            error: function(request) {

                                            },
                                            success: function(retdata) {

                                            }
                                        });
                                    }
                                }
                                if (this.deleteArray.length > 0) {
                                    for (var i = 0; i < this.deleteArray.length; i++) {
                                        var _this = this
                                        $.ajax({
                                            type: 'POST',
                                            url: 'http://192.168.1.66:808/api/purchasedetail/delete',
                                            data: {
                                                'token': localStorage.token,
                                                'userid': localStorage.userid,
                                                'fidno': _this.deleteArray[i],
                                            },
                                            dataType: 'json',
                                            crossDomain: true,
                                            error: function(request) {

                                            },
                                            success: function(retdata) {

                                            }
                                        });
                                    }
                                }
                                this.changeDetailShow()
                                this.deleteArray = []
                                this.oldDetail = []
                                this.newDetail = []
                            } else {
                                if (this.deleteArray.length > 0) {
                                    for (var i = 0; i < this.deleteArray.length; i++) {
                                        var _this = this
                                        $.ajax({
                                            type: 'POST',
                                            url: 'http://192.168.1.66:808/api/purchasedetail/delete',
                                            data: {
                                                'token': localStorage.token,
                                                'userid': localStorage.userid,
                                                'fidno': _this.deleteArray[i],
                                            },
                                            dataType: 'json',
                                            crossDomain: true,
                                            error: function(request) {

                                            },
                                            success: function(retdata) {

                                            }
                                        });
                                    }
                                }
                                var _this = this
                                $.ajax({
                                    type: 'POST',
                                    url: 'http://192.168.1.66:808/api/purchase/delete',
                                    data: {
                                        'token': localStorage.token,
                                        'userid': localStorage.userid,
                                        'fidno': _this.currentPurchase
                                    },
                                    dataType: 'json',
                                    crossDomain: true,
                                    error: function(request) {

                                    },
                                    success: function(retdata) {

                                    }
                                });
                                this.changeDetailShow()
                                this.deleteArray = []
                                this.oldDetail = []
                                this.newDetail = []
                            }
                        } else {
                            var _this = this
                            $.ajax({
                                type: 'POST',
                                url: 'http://192.168.1.66:808/api/purchase/add',
                                data: {
                                    'token': localStorage.token,
                                    'userid': localStorage.userid,
                                    'teamid': localStorage.activeteamid,
                                    'fcsid': '0',
                                    'ftitle': _this.title,
                                    'fnotes': _this.notes,
                                    'ftype': $('#ftype').val(),
                                    'fweixiuidno': $('#fixidno_sel').val()
                                },
                                dataType: 'json',
                                crossDomain: true,
                                error: function(request) {
                                    _this.$store.commit('showModal','连接错误')
                                },
                                success: function(retdata) {
                                    var purchaseids = retdata.Table2[0].fidno;
                                    for (i = 0; i < _this.newDetail.length; i++) {
                                        $.ajax({
                                            type: 'POST',
                                            url: 'http://192.168.1.66:808/api/purchasedetail/add',
                                            data: {
                                                'token': localStorage.token,
                                                'userid': localStorage.userid,
                                                'fidno2': purchaseids,
                                                'fitemidno': _this.newDetail[i].fitemid,
                                                'fqty': _this.newDetail[i].fqty,
                                                'fprice': _this.newDetail[i].fcost,
                                                'fsum': _this.newDetail[i].fsum,
                                                'fnotes': _this.newDetail[i].fnotes,
                                                'fpurchaseuser': _this.newDetail[i].purchaseuser,
                                                'fusedept': _this.newDetail[i].fusedept,
                                                'fusedevice': _this.newDetail[i].fusedevice
                                            },
                                            dataType: 'json',
                                            async: true,
                                            crossDomain: true,
                                            error: function(request) {

                                            },
                                            success: function(retdata) {

                                            }
                                        });
                                    }
                                    _this.changeDetailShow()
                                    _this.deleteArray = []
                                    _this.oldDetail = []
                                    _this.newDetail = []
                                }
                            });

                        }
                    }
                }
                this.getData()
            },
            print() {
                $('#notes').css('border', 'none')
                $('#detail_container').print({
                    noPrintSelector: ".print,.return,.save,.check",
                    globalStyles: true,
                    mediaPrint: false,
                    stylesheet: null,
                    iframe: false,
                    append: null,
                    prepend: null,
                    manuallyCopyFormValues: true,
                    deferred: $.Deferred()
                });
            },
            getDetail() {
                this.ifShowDetail = 'B'

                this.getsum()
            },
            getsum() {
                var totalsum = 0
                if (this.status == 'old') {
                    for (var i = 0; i < this.oldDetail.length; i++) {
                        this.oldDetail[i].fsum = (Number(this.oldDetail[i].fqty) * Number(this.oldDetail[i].fprice))
                        totalsum += this.oldDetail[i].fsum
                    }
                }
                for (var i = 0; i < this.newDetail.length; i++) {
                    this.newDetail[i].fsum = Number(this.newDetail[i].fqty) * Number(this.newDetail[i].fcost)
                    totalsum += this.newDetail[i].fsum
                }
                this.totalsum = totalsum
                this.getPurchaseNum()
            },
            changetotal() {
                var totalsum = 0
                $('td[name="sum"]').each(function() {
                    totalsum += Number($(this).html())
                })
                $('#sum').html(totalsum.toFixed(2))
            },
            getPurchaseNum() {
                var num = 0
                for (var i = 0; i < this.oldDetail.length; i++) {
                    num += Number(this.oldDetail[i].fqty)
                }
                for (var i = 0; i < this.newDetail.length; i++) {
                    num += Number(this.newDetail[i].fqty)
                }
                this.purchaseNum = num
            },
            getPurchaseuser(e) {
                var _this = this
                this.activeElement = e.currentTarget
                $('#purchaseuser_add_modal').modal('show');
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scaccount/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                    },
                    //async:false,
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误');
                    },
                    success: function(retdata) {
                        $('#purchaseuser_add_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                radio: true,
                            }, {
                                field: 'fidno',
                                title: '员工编号',
                            }, {
                                field: 'username',
                                title: '用户名'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'outteam',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: true,
                            //checkboxHeader: true,
                            radio: true,
                            //checkbox: true,
                            //clickToSelect: true,
                        });
                    }
                });
            },
            getUsedept(e) {
                var _this = this
                this.activeElement = e.currentTarget
                $('#usedept_add_modal').modal('show');
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误');
                    },
                    success: function(retdata) {
                        $('#usedept_add_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                radio: true,
                            }, {
                                field: 'fidno',
                                title: '部门编号',
                            }, {
                                field: 'fdname',
                                title: '部门名称'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'pdept',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: true,
                            //checkboxHeader: true,
                            radio: true,
                            //checkbox: true,
                            //clickToSelect: true,
                        });
                    }
                });
            },
            setUsedept() {
                var el = this.activeElement
                console.log(el)
                var id = $("input:radio[name='pdept']:checked").parent().next().text();
                var name = $("input:radio[name='pdept']:checked").parent().next().next().text();

                if ($('#checkforalldept').prop('checked') == false) {
                    if (this.oldDetail.length > 0) {
                        for (var i = 0; i < this.oldDetail.length; i++) {
                            if ($(el).parent().parent().children().eq(1).text() == this.oldDetail[i].fitemidno) {
                                this.oldDetail[i].fusedept = id
                                this.oldDetail[i].fusedeptname = name
                            }
                        }
                    }
                    if (this.newDetail.length > 0) {
                        for (var i = 0; i < this.newDetail.length; i++) {
                            if ($(el).parent().parent().children().eq(1).text() == this.newDetail[i].fitemid) {
                                this.newDetail[i].fusedept = id
                                this.newDetail[i].fusedeptname = name
                            }
                        }
                    }
                    // $(el).parent().next().children().val(id);
                    // $(el).val(name);
                } else {
                    $('tbody tr').each(function() {
                        // $(this).children().eq(13).children().val(name);
                        // $(this).children().eq(14).children().val(id);
                    })
                    if (this.oldDetail.length > 0) {
                        for (var i = 0; i < this.oldDetail.length; i++) {
                            this.oldDetail[i].fusedept = id
                            this.oldDetail[i].fusedeptname = name
                        }
                    }
                    if (this.newDetail.length > 0) {
                        for (var i = 0; i < this.newDetail.length; i++) {
                            this.newDetail[i].fusedept = id
                            this.newDetail[i].fusedeptname = name
                        }
                    }
                }
                $('#usedept_add_modal').modal('hide');
            },
            setPurchaseuser() {
                var el = this.activeElement
                console.log(el)
                var id = $("input:radio[name='outteam']:checked").parent().next().text();
                var name = $("input:radio[name='outteam']:checked").parent().next().next().text();

                if ($('#checkforall').prop('checked') == false) {
                    if (this.oldDetail.length > 0) {
                        for (var i = 0; i < this.oldDetail.length; i++) {
                            if ($(el).parent().parent().children().eq(1).text() == this.oldDetail[i].fitemidno) {
                                this.oldDetail[i].fpurchaseuser = id
                                this.oldDetail[i].fpurchaseusername = name
                            }
                        }
                    }
                    if (this.newDetail.length > 0) {
                        for (var i = 0; i < this.newDetail.length; i++) {
                            if ($(el).parent().parent().children().eq(1).text() == this.newDetail[i].fitemid) {
                                this.newDetail[i].purchaseuser = id
                                this.newDetail[i].purchaseusername = name
                            }
                        }
                    }
                    // $(el).parent().next().children().val(id);
                    // $(el).val(name);
                } else {
                    $('tbody tr').each(function() {
                        // $(this).children().eq(13).children().val(name);
                        // $(this).children().eq(14).children().val(id);
                    })
                    if (this.oldDetail.length > 0) {
                        for (var i = 0; i < this.oldDetail.length; i++) {
                            this.oldDetail[i].fpurchaseuser = id
                            this.oldDetail[i].fpurchaseusername = name
                        }
                    }
                    if (this.newDetail.length > 0) {
                        for (var i = 0; i < this.newDetail.length; i++) {
                            this.newDetail[i].purchaseuser = id
                            this.newDetail[i].purchaseusername = name
                        }
                    }
                }
                $('#purchaseuser_add_modal').modal('hide');
            },
            deleteItem() {
                while (this.oldcheckedids.length > 0) {
                    for (var i = 0; i < this.oldDetail.length; i++) {
                        var a = this.oldcheckedids.indexOf(this.oldDetail[i].fidno)
                        if (a > -1) {
                            this.deleteArray.push(this.oldDetail[i].fidno)
                            this.oldDetail.splice(i, 1)
                            this.oldcheckedids.splice(a, 1)
                            break
                        }
                    }
                }
                while (this.newcheckedids.length > 0) {
                    for (var i = 0; i < this.newDetail.length; i++) {
                        var a = this.newcheckedids.indexOf(this.newDetail[i].fitemid)
                        if (a > -1) {
                            this.newDetail.splice(i, 1)
                            this.newcheckedids.splice(a, 1)
                            break
                        }
                    }
                }
                this.getsum()
                this.getPurchaseNum()
            },
            getFixs() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assetsfix/fetchallfixbyuserid',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'extra.selectfield': '*',
                        'extra.sortfield': 'fidno',
                        'extra.pagesize': 9999999,
                        'extra.pageindex': 1,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },

                    success: function(retdata) {
                        if (retdata.Table1[0].status == false) {
                            _this.$store.commit('showModal','您未进行报修，请先报修再请购物品！')
                        } else {
                            var htmlcont = '';

                            function option(fidno, fname) {
                                this.fidno = fidno
                                this.fname = fname
                            }
                            var array = []
                            for (var i = 0; i < retdata.Table2.length; i++) {
                                if (retdata.Table2[i].fisok == false) {
                                    var opt = new option()
                                    opt.fidno = retdata.Table2[i].fidno
                                    opt.fname = retdata.Table2[i].fname
                                    array.push(opt)
                                }
                            }
                            _this.options = array
                            if (array.length == 0) {
                                _this.$store.commit('showModal','您最近未进行报修，请先报修再请购物品！')
                                $('#ftype').val('日常采购');
                                _this.showfix = false
                            } else {
                                $('#fixidno_sel').html(htmlcont);
                            }
                        }
                    }
                });
            }
        },
        mounted() {
            this.getData()
        },
        watch: {
            'status': function() {
                if (this.status == 'old') {
                    // this.date = 
                }
            },

        }
    }
</script>
<style>
    #operation {
        float: left;
        margin-bottom: 5px;
    }
    
    #detail_container td,
    #detail_container th,
    #detail_container input {
        padding-left: 0px;
        padding-right: 0px;
        text-align: center;
        /*border: 0px;*/
    }
    
    #detail_container h4 {
        margin-top: 0px;
        margin-bottom: 0px;
    }
    
    #notes {
        padding: 0px;
        text-indent: 30px;
        border: 1px solid #eee;
    }
    
    .table td {
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: keep-all;
        white-space: nowrap;
    }
</style>